package com.sumu.today.models.permission.controller;

import com.baomidou.kaptcha.Kaptcha;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sumu.today.models.permission.entity.User;
import com.sumu.today.models.permission.service.UserService;
import com.sumu.today.models.permission.utils.SSOUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.UUID;


/**
 * @author sumu
 * @date 12/15/2021 9:19 PM
 */
@Slf4j
@Controller
@RequestMapping("/permission")
public class loginController {

    @Autowired
    UserService userService;

    @Autowired
    private Kaptcha kaptcha;


    @GetMapping("/login")
    public ModelAndView login(ModelAndView modelAndView, HttpServletRequest request) {
        log.info("go to login page !!");
        User res = (User) request.getSession().getAttribute("loginUser");
        if (res != null) {
            modelAndView.addObject("msg", res.getName() + " already login !!!");
            modelAndView.setViewName("index");
            return modelAndView;
        } else {
            modelAndView.setViewName("permission/login");
            return modelAndView;
        }
    }

    @GetMapping("/unLogin")
    public ModelAndView unLogin(ModelAndView modelAndView, HttpServletRequest request) {
        User loginUser = (User) request.getSession().getAttribute("loginUser");
        if (loginUser == null) {
            modelAndView.addObject("msg", "当前用户尚未登录，请登录。");
            modelAndView.setViewName("permission/login");
            return modelAndView;
        } else {
//            request.getSession().removeAttribute("loginUser");
            request.getSession().invalidate();
            modelAndView.addObject("sessionsOut", loginUser.getName() + " 退出登录 \n");
            modelAndView.setViewName("index");
            return modelAndView;
        }

    }


    @PostMapping("/login")
    public ModelAndView isLogin(ModelAndView modelAndView, @Validated User user, BindingResult bindingResult, String kaptchaCode, HttpServletRequest request) throws Exception{

        log.info("验证码: " + kaptchaCode);
        kaptcha.validate(kaptchaCode);
        boolean login = userService.login(user, request);

        if (bindingResult.hasErrors()) {
            modelAndView.addObject("error", bindingResult.getFieldError().getDefaultMessage());
            modelAndView.setViewName("permission/login");
            return modelAndView;
        }
        if (login) {
            modelAndView.addObject("msg", "login success !!!");
            modelAndView.setViewName("index");
            return modelAndView;
        } else {
            modelAndView.addObject("msg", "login failed !!!");
            modelAndView.addObject("error", "用户或者密码错误，请重新输入!!!");
            modelAndView.setViewName("permission/login");
            return modelAndView;
        }

    }

    public void test() {
        QueryWrapper queryWrapper = new QueryWrapper(new User("大龙"));
        User userServiceOne = userService.getOne(queryWrapper);

        System.out.println(userServiceOne);
    }
}
